Linux下使用fdisk扩大分区容量(不丢失数据)

您所在的位置:网站首页 linux 磁盘扩展分区 Linux下使用fdisk扩大分区容量(不丢失数据)

Linux下使用fdisk扩大分区容量(不丢失数据)

2024-07-07 21:04| 来源: 网络整理| 查看: 265

环境:

模拟新加一块磁盘,并分区,挂载,永久挂载,然后写入数据,然后对分区扩容,测试扩容后分区里面的内容是否丢失。

查看所有分区:

[root@backup ~]# fdisk -l Disk /dev/sda: 1073.7 GB, 1073741824000 bytes 255 heads, 63 sectors/track, 130541 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00040b96    Device Boot      Start         End      Blocks   Id  System /dev/sda1   *           1          26      204800   83  Linux Partition 1 does not end on cylinder boundary. /dev/sda2              26        1070     8388608   82  Linux swap / Solaris /dev/sda3            1070       13055    96263168   83  Linux Disk /dev/sdb: 1073.7 GB, 1073741824000 bytes 255 heads, 63 sectors/track, 130541 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000

开始分区:

[root@backup ~]# fdisk /dev/sdb Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0xc6e44e94. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) WARNING: DOS-compatible mode is deprecated. It's strongly recommended to          switch off the mode (command 'c') and change display units to          sectors (command 'u'). Command (m for help): p            #p是打印当前磁盘所有分区 Disk /dev/sdb: 1073.7 GB, 1073741824000 bytes 255 heads, 63 sectors/track, 130541 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0xc6e44e94    Device Boot      Start         End      Blocks   Id  System Command (m for help): n            #n是新建分区 Command action    e   extended    p   primary partition (1-4) p                                  #新建主分区 Partition number (1-4): 1          #给主分区编号为1,从1开始编号 First cylinder (1-130541, default 1):     #回车,默认从1开始,也可以手动输入1 Using default value 1                        #起始分区的位置 Last cylinder, +cylinders or +size{K,M,G} (1-130541, default 130541):     #直接回车,默认把磁盘所有空间分配到此分区。这里支持K,M,G,如果想给此分区分配100M空间,就输入+100M;如果要分配100G那么就输入+100G Using default value 130541                    #结束分区的位置 Command (m for help): w                #w为写入分区信息并保存,然后退出 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.

查看刚刚分区的信息:/dev/sdb1

[root@backup ~]# fdisk -l Disk /dev/sda: 1073.7 GB, 1073741824000 bytes 255 heads, 63 sectors/track, 130541 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00040b96    Device Boot      Start         End      Blocks   Id  System /dev/sda1   *           1          26      204800   83  Linux Partition 1 does not end on cylinder boundary. /dev/sda2              26        1070     8388608   82  Linux swap / Solaris /dev/sda3            1070       13055    96263168   83  Linux Disk /dev/sdb: 1073.7 GB, 1073741824000 bytes 255 heads, 63 sectors/track, 130541 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0xc6e44e94    Device Boot      Start         End      Blocks   Id  System /dev/sdb1               1      130541  1048570551   83  Linux

格式化分区:mkfs.ext4 /dev/sdb1

[root@backup ~]# mkfs.ext4 /dev/sdb1 mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 65536000 inodes, 262142637 blocks 13107131 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=4294967296 8000 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks:          32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,          4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,          102400000, 214990848 Writing inode tables: done                             Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information:  done This filesystem will be automatically checked every 26 mounts or 180 days, whichever comes first.  Use tune2fs -c or -i to override.

挂载分区:

[root@backup backup]# mkdir /backup [root@backup backup]# mount /dev/sdb1 /backup/ [root@backup backup]# df -hT Filesystem     Type   Size  Used Avail Use% Mounted on /dev/sda3      ext4    91G  2.2G   84G   3% / tmpfs          tmpfs  1.9G     0  1.9G   0% /dev/shm /dev/sda1      ext4   190M   38M  142M  21% /boot /dev/sdb1      ext4   985G   72M  935G   1% /backup

挂载磁盘:(永久挂载)

   通过vi编辑器编辑/etc/fstab文件,内容如下,(最后一行是刚添加上去的):

[root@backup ~]# cat /etc/fstab  # # /etc/fstab # Created by anaconda on Mon May 14 02:01:11 2018 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=7d223255-1b77-4f58-bd72-262928547fcf /                       ext4    defaults        1 1 UUID=284d6b31-8723-497b-bc7b-3c12ede23ee6 /boot                   ext4    defaults        1 2 UUID=5d36c1b3-df6e-480d-80ca-72befc66f5e9 swap                    swap    defaults        0 0 tmpfs                   /dev/shm                tmpfs   defaults        0 0 devpts                  /dev/pts                devpts  gid=5,mode=620  0 0 sysfs                   /sys                    sysfs   defaults        0 0 proc                    /proc                   proc    defaults        0 0 /dev/sdb1                   /backup                 ext4    defaults        0 0

第四列挂载参数:通过查看man mount 来查看

第五列是否要备份:(0为不备份,1为要备份,一般情况下不用做备份)

第六列自检程序  (0为不自检,1或2为要自检,如果是根分区要设置1,其它分区只能是2)

[root@backup ~]# echo backup >/backup/backup.txt [root@backup ~]# cat /backup/backup.txt  backup

此时机器不关机,用vmware vspere client扩容磁盘大小。

[root@backup ~]# fdisk -l /dev/sdb Disk /dev/sdb: 1073.7 GB, 1073741824000 bytes 255 heads, 63 sectors/track, 130541 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0xc6e44e94    Device Boot      Start         End      Blocks   Id  System /dev/sdb1               1      130541  1048570551   83  Linux

此时发现扩容的磁盘容量看不到,所以还是重启一下吧,建议关机时候再扩容。

[root@backup ~]# umount /dev/sdb1    #取消挂载 [root@backup ~]# fdisk /dev/sdb     WARNING: DOS-compatible mode is deprecated. It's strongly recommended to          switch off the mode (command 'c') and change display units to          sectors (command 'u'). Command (m for help): p Disk /dev/sdb: 1181.1 GB, 1181116006400 bytes 255 heads, 63 sectors/track, 143595 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0xc6e44e94    Device Boot      Start         End      Blocks   Id  System /dev/sdb1               1      130541  1048570551   83  Linux Command (m for help): d        #因为此磁盘只有一个分区sdb1,所以按d删除时候默认不会让选择要删除的分区,如果有多个分区会提示要删除的分区。 Selected partition 1 Command (m for help): p        #打印当前分区,发现分区已删除 Disk /dev/sdb: 1181.1 GB, 1181116006400 bytes 255 heads, 63 sectors/track, 143595 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0xc6e44e94    Device Boot      Start         End      Blocks   Id  System Command (m for help): n        #新建分区 Command action    e   extended    p   primary partition (1-4) p                                #新建主分区 Partition number (1-4): 1        #和扩容前的分区标号必须一样 First cylinder (1-143595, default 1):     #默认是1,比如要和扩容前的分区其实点一样,扩容前的分区起始也是1,这个是扩容的重点地方。 Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-143595, default 143595): #终止的位置是默认是硬盘柱体的尾部,即使最大值,这个是要扩容的点,回车。 Using default value 143595 Command (m for help): w                           #w报错,并退出。 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.

3、调整分区

e2fsck -f /dev/sdb1 #检查分区信息

resize2fs /dev/sdb1 #调整分区大小

[root@backup ~]# e2fsck -f /dev/sdb1    #由于磁盘容量很大,数据多的情况下需要等待一段时间 e2fsck 1.41.12 (17-May-2010) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information /dev/sdb1: 12/65536000 files (0.0% non-contiguous), 4164250/262142637 blocks [root@backup ~]# resize2fs /dev/sdb1     #由于磁盘容量很大,数据多的情况下需要等待一段时间 resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/sdb1 to 288356701 (4k) blocks. The filesystem on /dev/sdb1 is now 288356701 blocks long. [root@backup ~]# mount /dev/sdb1 /backup/ [root@backup ~]# cat /backup/ backup.txt  lost+found/  [root@backup ~]# cat /backup/backup.txt  backup

发现扩容成功,扩容前的内容没有丢失。

参考内容:

https://blog.csdn.net/wuweilong/article/details/7538634

https://blog.csdn.net/jackyu651/article/details/53070607

https://blog.csdn.net/a746742897/article/details/52689285



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3